<?php
//loading zend framework path
$clientLibraryPath = 'ZendGdata-1.11.2/library';
$oldPath = set_include_path(get_include_path() . PATH_SEPARATOR . $clientLibraryPath);$clientLibraryPath = '/Users/anu/DevApps/ZendGdata-1.11.2/library';
$oldPath = set_include_path(get_include_path() . PATH_SEPARATOR . $clientLibraryPath);


   // load Zend Gdata libraries
   require_once 'Zend/Loader.php';
   Zend_Loader::loadClass('Zend_Gdata_Books');
   Zend_Loader::loadClass('Zend_Gdata_Books_VolumeQuery');
   Zend_Loader::loadClass('Zend_Gdata_ClientLogin');

   try {
       // perform login -- NO NEED --
       //$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, 'print');
       //$client->setHeaders('X-Forwarded-For', $_SERVER['REMOTE_ADDR']);
       $books = new Zend_Gdata_Books();

       // prepare and execute search query
       $query = new Zend_Gdata_Books_VolumeQuery;
       $query->setQuery(urlencode($_GET['q']));
       $feed = $books->getVolumeFeed($query);  
   } catch (Exception $e) {
       	 die('ERROR:' . $e->getMessage()); }

?>
<!DOCTYPE html 
  PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <title>Searching for book titles</title>
    <style>
    .entry {
      margin-bottom:5px;
    }
    .thumbnail {
      float: left;  
      //border: solid black 2px;
      //padding: 2px;
      //margin-right: 5px;
    }
    .desc {
    	  font-style: italic;
    }
    .small {
    	   font-size: smaller;
    }
    </style>
  </head>
  <body>
    
    <?php if (isset($feed)): ?>
  
	<div id="results">
    	<?php $x = 1; ?>
    	<?php foreach ($feed as $entry): ?>
	      <?php
	      //print_r($entry);
	      $book = new stdClass;

	      //get title
	      if(is_array($entry->getTitles())) 
	      {
		foreach($entry->getTitles() as $title) 
		{
			$book->titles[] = $title->getText();
		}									
	      }

	      //get authors
	      if(is_array($entry->getCreators()))
	      {
		foreach($entry->getCreators() as $creator) 
		{
			$book->authors[] = $creator->getText();
		}
	      }

	      //get publishers
	      if(is_array($entry->getPublishers()))
	      {
		foreach($entry->getPublishers() as $publisher)
		{
			$book->publishers[] = $publisher->getText();
		}
	      }

	      //get publication dates
	      if(is_array($entry->getDates()))
	      {
		$arr = $entry->getDates();
		$book->pubdate = (is_object($arr[0])) ? 
		  $arr[0]->getText() : 'Date no available';
	      }

	      //get ISBN number
	      if(is_array($entry->getIdentifiers()))
	      {
		foreach($entry->getIdentifiers() as $id)
		{
			if(preg_match('/ISBN/', $id->getText()))
			{
				$book->isbn[]= $id->getText();
			}
		}
	      }

	      //get first subject
	      if(is_array($entry->getSubjects()))
	      {
		$arr = $entry->getSubjects();
		$book->subject = is_object($arr[0]) ? 
		  $arr[0]->getText() : 'Subject is not available';
	      }

	      //get first description
	      if(is_array($entry->getDescriptions()))
	      {
		$arr = $entry->getDescriptions();
		$book->desc = is_object($arr[0]) ? $arr[0]->getText() : '';
	      }
	      ?>
	      <div class="entry">
		   <div class="data">
		             <?php echo $x; ?>. <?php echo ucwords(@implode(': ',  $book->titles)); ?></br>
          		     <?php echo @implode(', ', $book->authors); ?> |
          		     <?php echo @implode(', ', $book->publishers); ?> |
          		     <?php echo $book->subject; ?> |
          		     <?php echo date('d M Y', strtotime($book->pubdate)); ?> <br/>
          		     <?php if($book->desc) { ?><span class="desc"><?php echo $book->desc; ?></span> <br/><?php } ?>
          		     <span class="small"><?php echo @implode(', ', $book->isbn); ?> |       
            		     <a href="<?php echo $entry->getInfoLink()->getHref(); ?>">More information</a>   
          		     </span>
		    </div>
	      </div>
	   <?php $x++; ?>     	     
        <?php endforeach; ?>  
    <?php endif; ?>  
    
  </body>
</html>
